package starComponent.experiments;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import starComponent.experiments.expedependencies.E_Experiment;
import starComponent.experiments.expedependencies.Expe1Dependencies_Training;



/**
 * @author Charles Perin
 * 9 juin 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class ExperimentsLauncher {

	public static final int FRAME_WIDTH = 800;
	public static final int FRAME_HEIGHT = 600;

	public static final int EXPE1 = 0;
	public static final int EXPE2 = 1;
	public static final int EXPE3 = 2;
	public static final int EXPE4 = 3;

	public static final int SCENARIO1 = 0;
	public static final int SCENARIO2 = 1;
	public static final int SCENARIO3 = 2;
	public static final int SCENARIO4 = 3;

	public static final int CONDITION1 = 0;
	public static final int CONDITION2 = 1;
	public static final int CONDITION3 = 2;
	public static final int CONDITION4 = 3;
	public static final int CONDITION5 = 4;
	public static final int CONDITION6 = 5;



	public static final int PARALLEL = 0;
	public static final int STAR = 1;





	private static final Dimension buttonDim = new Dimension(200,30);
	private static JPanel staticPanel;
	private static JPanel dynamicPanel1;
	private static JPanel dynamicPanel2;
	private static JFrame frame;
	private static int groupNumber = -1;
	private static ArrayList<ListenerButtonsLauncher> listenerButtonList = new ArrayList<ListenerButtonsLauncher>();

	public static void main(String[] args){

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				frame = new JFrame("Experiments Chooser");
				frame.setPreferredSize(new Dimension(800,600));
				frame.setLocation((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2)-FRAME_WIDTH/2, (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2)-FRAME_HEIGHT/2);
				frame.setUndecorated(false);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.getContentPane().setLayout(new GridBagLayout());
				frame.setVisible(true);
				frame.pack();
				initFrame();
			}
		});
	}

	private static void initFrame() {
		initStaticPanel();
		dynamicPanel1 = new JPanel();
		dynamicPanel1.setPreferredSize(new Dimension(frame.getContentPane().getWidth()/3,frame.getContentPane().getHeight()/2));
		dynamicPanel1.setLayout(new FlowLayout());

		dynamicPanel2 = new JPanel();
		dynamicPanel2.setPreferredSize(new Dimension(frame.getContentPane().getWidth()/3,frame.getContentPane().getHeight()/2));
		dynamicPanel2.setLayout(new FlowLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.gridx=0;
		c.gridy=0;
		c.gridwidth=1;
		c.gridheight=1;
		c.weightx=1;
		c.weighty=1;
		c.fill = GridBagConstraints.BOTH;

		frame.add(staticPanel,c);

		c.gridx+=1;
		frame.add(dynamicPanel1,c);

		c.gridx+=1;
		frame.add(dynamicPanel2,c);

	}

	private static void initStaticPanel(){
		staticPanel = new JPanel();
		staticPanel.setPreferredSize(new Dimension(frame.getContentPane().getWidth()/3,frame.getContentPane().getHeight()/2));
		staticPanel.setBorder(BorderFactory.createTitledBorder("Experiment:"));



		JButton expe1 = new JButton("1: "+E_Experiment.DEPENDENCIES_SCENARIO1_INTERVALLES.getTitle());
		expe1.setPreferredSize(buttonDim);
		expe1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				updateDynamicPanel1(EXPE1);
			}
		});

		JButton expe2 = new JButton("2: "+E_Experiment.COMPARISON_SCENARIO1_PARALLEL.getTitle());
		expe2.setPreferredSize(buttonDim);
		expe2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				updateDynamicPanel1(EXPE2);
			}
		});

		JButton expe3 = new JButton("3: "+E_Experiment.OCCLUSION_SCENARIO1_PARALLEL.getTitle());
		expe3.setPreferredSize(buttonDim);
		expe3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				updateDynamicPanel1(EXPE3);
			}
		});

		JButton expe4 = new JButton("4: "+"?");
		expe4.setPreferredSize(buttonDim);
		expe4.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				updateDynamicPanel1(EXPE4);
			}
		});


		JLabel groupLabel = new JLabel("Group Number :");
		groupLabel.setPreferredSize(buttonDim);

		final JComboBox groupSelect = new JComboBox();
		groupSelect.setPreferredSize(buttonDim);
		String[]groups = new String[]{"select","1","2","3","4","5","6"};


		for(int i=0;i<groups.length;i++){
			groupSelect.addItem(groups[i]);
		}
		groupSelect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				String selected = (String)(groupSelect.getSelectedItem());
				if(selected.equals("select")){
					groupNumber=-1;
				}
				else{
					groupNumber = Integer.parseInt(selected);
				}
				for(ListenerButtonsLauncher lbl:listenerButtonList){
					lbl.setGroupNumber(groupNumber);
				}
			}
		});


		staticPanel.setLayout(new FlowLayout());

		staticPanel.add(groupLabel);

		staticPanel.add(groupSelect);

		staticPanel.add(expe1);

		staticPanel.add(expe2);

		staticPanel.add(expe3);

		staticPanel.add(expe4);
	}





	private static void updateDynamicPanel1(int expe){
		dynamicPanel1.removeAll();
		dynamicPanel2.removeAll();

		JButton expe1_training = new JButton("Training");
		expe1_training.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				new Expe1Dependencies_Training();
			}
		});

		switch(expe){
		case EXPE1:
			dynamicPanel1.setBorder(BorderFactory.createTitledBorder(E_Experiment.DEPENDENCIES_SCENARIO1_INTERVALLES.getTitle()));
			JButton expe1_scenario1 = new JButton(E_Experiment.DEPENDENCIES_SCENARIO1_INTERVALLES.getScenarioTitle());
			expe1_scenario1.setPreferredSize(buttonDim);
			expe1_scenario1.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					updateDynamicPanel2(EXPE1, SCENARIO1);
				}
			});

			JButton expe1_scenario2 = new JButton(E_Experiment.DEPENDENCIES_SCENARIO2_INTERVALLES.getScenarioTitle());
			expe1_scenario2.setPreferredSize(buttonDim);
			expe1_scenario2.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					updateDynamicPanel2(EXPE1, SCENARIO2);
				}
			});

			JButton expe1_scenario3 = new JButton(E_Experiment.DEPENDENCIES_SCENARIO3_INTERVALLES.getScenarioTitle());
			expe1_scenario3.setPreferredSize(buttonDim);
			expe1_scenario3.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					updateDynamicPanel2(EXPE1, SCENARIO3);
				}
			});

			JButton expe1_scenario4 = new JButton(E_Experiment.DEPENDENCIES_SCENARIO4_INTERVALLES.getScenarioTitle());
			expe1_scenario4.setPreferredSize(buttonDim);
			expe1_scenario4.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					updateDynamicPanel2(EXPE1, SCENARIO4);
				}
			});

			dynamicPanel1.add(expe1_training);
			dynamicPanel1.add(expe1_scenario1);
			dynamicPanel1.add(expe1_scenario2);
			dynamicPanel1.add(expe1_scenario3);
			dynamicPanel1.add(expe1_scenario4);
			break;

		case EXPE2:
			dynamicPanel1.setBorder(BorderFactory.createTitledBorder(E_Experiment.COMPARISON_SCENARIO1_PARALLEL.getTitle()));
			JButton expe2_parallel = new JButton("Parallel");
			expe2_parallel.setPreferredSize(buttonDim);
			expe2_parallel.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					updateDynamicPanel2(EXPE2, PARALLEL);
				}
			});

			JButton expe2_star = new JButton("Star");
			expe2_star.setPreferredSize(buttonDim);
			expe2_star.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					updateDynamicPanel2(EXPE2, STAR);
				}
			});



			dynamicPanel1.add(expe2_parallel);


			dynamicPanel1.add(expe2_star);


			break;

		case EXPE3:
			dynamicPanel1.setBorder(BorderFactory.createTitledBorder(E_Experiment.OCCLUSION_SCENARIO1_PARALLEL.getTitle()));

			

			break;

		case EXPE4:
			dynamicPanel1.setBorder(BorderFactory.createTitledBorder("?"));
			//TODO expe 4

			break;

		default: System.err.println("erreur dynamicPanel1");
		}
		dynamicPanel1.revalidate();

	}



	private static void updateDynamicPanel2(int expe, int scenario){
		dynamicPanel2.removeAll();
		listenerButtonList.clear();

		switch(expe){
		case EXPE1:

			JButton condition1 = new JButton("Condition 1");
			JButton condition2 = new JButton("Condition 2");
			JButton condition3 = new JButton("Condition 3");

			condition1.setPreferredSize(buttonDim);
			condition2.setPreferredSize(buttonDim);
			condition3.setPreferredSize(buttonDim);

			switch(scenario){
			case SCENARIO1:
				dynamicPanel2.setBorder(BorderFactory.createTitledBorder(E_Experiment.DEPENDENCIES_SCENARIO1_INTERVALLES.getScenarioTitle()));

				ListenerButtonsLauncher listener11 = new ListenerButtonsLauncher(EXPE1, SCENARIO1, CONDITION1, groupNumber);
				listenerButtonList.add(listener11);
				condition1.addActionListener(listener11);

				ListenerButtonsLauncher listener12 = new ListenerButtonsLauncher(EXPE1, SCENARIO1, CONDITION2, groupNumber);
				listenerButtonList.add(listener12);
				condition2.addActionListener(listener12);

				ListenerButtonsLauncher listener13 = new ListenerButtonsLauncher(EXPE1, SCENARIO1, CONDITION3, groupNumber);
				listenerButtonList.add(listener13);
				condition3.addActionListener(listener13);

				break;

			case SCENARIO2:
				dynamicPanel2.setBorder(BorderFactory.createTitledBorder(E_Experiment.DEPENDENCIES_SCENARIO2_INTERVALLES.getScenarioTitle()));

				ListenerButtonsLauncher listener21 = new ListenerButtonsLauncher(EXPE1, SCENARIO2, CONDITION1, groupNumber);
				listenerButtonList.add(listener21);
				condition1.addActionListener(listener21);

				ListenerButtonsLauncher listener22 = new ListenerButtonsLauncher(EXPE1, SCENARIO2, CONDITION2, groupNumber);
				listenerButtonList.add(listener22);
				condition2.addActionListener(listener22);

				ListenerButtonsLauncher listener23 = new ListenerButtonsLauncher(EXPE1, SCENARIO2, CONDITION3, groupNumber);
				listenerButtonList.add(listener23);
				condition3.addActionListener(listener23);

				break;

			case SCENARIO3:
				dynamicPanel2.setBorder(BorderFactory.createTitledBorder(E_Experiment.DEPENDENCIES_SCENARIO3_INTERVALLES.getScenarioTitle()));

				ListenerButtonsLauncher listener31 = new ListenerButtonsLauncher(EXPE1, SCENARIO3, CONDITION1, groupNumber);
				listenerButtonList.add(listener31);
				condition1.addActionListener(listener31);

				ListenerButtonsLauncher listener32 = new ListenerButtonsLauncher(EXPE1, SCENARIO3, CONDITION2, groupNumber);
				listenerButtonList.add(listener32);
				condition2.addActionListener(listener32);

				ListenerButtonsLauncher listener33 = new ListenerButtonsLauncher(EXPE1, SCENARIO3, CONDITION3, groupNumber);
				listenerButtonList.add(listener33);
				condition3.addActionListener(listener33);

				break;

			case SCENARIO4:
				dynamicPanel2.setBorder(BorderFactory.createTitledBorder(E_Experiment.DEPENDENCIES_SCENARIO4_INTERVALLES.getScenarioTitle()));

				ListenerButtonsLauncher listener41 = new ListenerButtonsLauncher(EXPE1, SCENARIO4, CONDITION1, groupNumber);
				listenerButtonList.add(listener41);
				condition1.addActionListener(listener41);

				ListenerButtonsLauncher listener42 = new ListenerButtonsLauncher(EXPE1, SCENARIO4, CONDITION2, groupNumber);
				listenerButtonList.add(listener42);
				condition2.addActionListener(listener42);

				ListenerButtonsLauncher listener43 = new ListenerButtonsLauncher(EXPE1, SCENARIO4, CONDITION3, groupNumber);
				listenerButtonList.add(listener43);
				condition3.addActionListener(listener43);

				break;

			default:
				System.err.println("erreur updateDynamicPanel2 scenario");
			}

			dynamicPanel2.add(condition1);
			dynamicPanel2.add(condition2);
			dynamicPanel2.add(condition3);

			break;


		case EXPE2:
			
			JButton condition11 = new JButton("Condition 1");
			JButton condition12 = new JButton("Condition 2");
			JButton condition13 = new JButton("Condition 3");
			JButton condition14 = new JButton("Condition 4");
			JButton condition15 = new JButton("Condition 5");
			JButton condition16 = new JButton("Condition 6");

			condition11.setPreferredSize(buttonDim);
			condition12.setPreferredSize(buttonDim);
			condition13.setPreferredSize(buttonDim);
			condition14.setPreferredSize(buttonDim);
			condition15.setPreferredSize(buttonDim);
			condition16.setPreferredSize(buttonDim);

			switch(scenario){

			case PARALLEL:
				dynamicPanel2.setBorder(BorderFactory.createTitledBorder("Parallel Coordinates"));

				ListenerButtonsLauncher listener11 = new ListenerButtonsLauncher(EXPE2, PARALLEL, CONDITION1, groupNumber);
				listenerButtonList.add(listener11);
				condition11.addActionListener(listener11);

				ListenerButtonsLauncher listener12 = new ListenerButtonsLauncher(EXPE2, PARALLEL, CONDITION2, groupNumber);
				listenerButtonList.add(listener12);
				condition12.addActionListener(listener12);

				ListenerButtonsLauncher listener13 = new ListenerButtonsLauncher(EXPE2, PARALLEL, CONDITION3, groupNumber);
				listenerButtonList.add(listener13);
				condition13.addActionListener(listener13);
				
				ListenerButtonsLauncher listener14 = new ListenerButtonsLauncher(EXPE2, PARALLEL, CONDITION4, groupNumber);
				listenerButtonList.add(listener14);
				condition14.addActionListener(listener14);

				ListenerButtonsLauncher listener15 = new ListenerButtonsLauncher(EXPE2, PARALLEL, CONDITION5, groupNumber);
				listenerButtonList.add(listener15);
				condition15.addActionListener(listener15);

				ListenerButtonsLauncher listener16 = new ListenerButtonsLauncher(EXPE2, PARALLEL, CONDITION6, groupNumber);
				listenerButtonList.add(listener16);
				condition16.addActionListener(listener16);
				
				break;

			case STAR:
				dynamicPanel2.setBorder(BorderFactory.createTitledBorder("Star"));

				ListenerButtonsLauncher listener21 = new ListenerButtonsLauncher(EXPE2, STAR, CONDITION1, groupNumber);
				listenerButtonList.add(listener21);
				condition11.addActionListener(listener21);

				ListenerButtonsLauncher listener22 = new ListenerButtonsLauncher(EXPE2, STAR, CONDITION2, groupNumber);
				listenerButtonList.add(listener22);
				condition12.addActionListener(listener22);

				ListenerButtonsLauncher listener23 = new ListenerButtonsLauncher(EXPE2, STAR, CONDITION3, groupNumber);
				listenerButtonList.add(listener23);
				condition13.addActionListener(listener23);
				
				ListenerButtonsLauncher listener24 = new ListenerButtonsLauncher(EXPE2, STAR, CONDITION4, groupNumber);
				listenerButtonList.add(listener24);
				condition14.addActionListener(listener24);

				ListenerButtonsLauncher listener25 = new ListenerButtonsLauncher(EXPE2, STAR, CONDITION5, groupNumber);
				listenerButtonList.add(listener25);
				condition15.addActionListener(listener25);

				ListenerButtonsLauncher listener26 = new ListenerButtonsLauncher(EXPE2, STAR, CONDITION6, groupNumber);
				listenerButtonList.add(listener26);
				condition16.addActionListener(listener26);
				break;


			}
			
			dynamicPanel2.add(condition11);
			dynamicPanel2.add(condition12);
			dynamicPanel2.add(condition13);
			dynamicPanel2.add(condition14);
			dynamicPanel2.add(condition15);
			dynamicPanel2.add(condition16);

			break;

		case EXPE3:
			//TODO EXPE3
			break;

		case EXPE4:
			//TODO EXPE4
			break;

		default:
			System.err.println("erreur updateDynamicPanel2 expe");
		}

		dynamicPanel2.revalidate();

	}
}
